package com.scf.fruit.business.hospital.bo;

import com.github.pagehelper.PageHelper;
import com.scf.fruit.business.hospital.po.request.HospitalVisitReq;
import com.scf.fruit.business.hospital.po.response.HospitalVisitRes;
import com.scf.fruit.hospital.HospitalVisitDto;
import com.scf.fruit.hospital.HospitalVisitService;
import com.scf.fruit.shiro.FruitUserService;
import com.scf.fruit.user.FruitUserDto;
import com.scf.fruit.util.DataTablesRes;
import com.scf.fruit.util.SpringCglibBeanUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author : 孙超飞
 * @title :
 * @description :
 * @date : 2018-04-23 9:15
 */
@Component
public class HospitalVisitBo {
    @Resource
    private HospitalVisitService hospitalVisitService;
    @Resource
    private FruitUserService fruitUserService;
    List<HospitalVisitDto> hospitalVisitDtoList=new ArrayList<>();
    public DataTablesRes queryList(HospitalVisitReq req){
        PageHelper.startPage(req.getPageIndex(),req.getLength(),false);
        HospitalVisitDto convert = SpringCglibBeanUtils.convert(req, HospitalVisitDto.class);
        List<HospitalVisitDto> hospitalVisitDtos = hospitalVisitService.queryList(convert);
        hospitalVisitDtoList=hospitalVisitService.queryList(convert);
        Integer count=hospitalVisitService.count(convert);
        DataTablesRes res=new DataTablesRes(req.getDraw(),count,count,SpringCglibBeanUtils.convertByList(hospitalVisitDtos, HospitalVisitRes.class),null);
        return res;
    }

    public Boolean insert(HospitalVisitReq req){
        String s = SecurityUtils.getSubject().getPrincipal().toString();
        FruitUserDto byMobile = fruitUserService.findByMobile(s);
        req.setUserName(byMobile.getNickName());
        return hospitalVisitService.insert(SpringCglibBeanUtils.convert(req, HospitalVisitDto.class));
    }

    public Boolean update(HospitalVisitReq req){
        String s = SecurityUtils.getSubject().getPrincipal().toString();
        FruitUserDto byMobile = fruitUserService.findByMobile(s);
        req.setUserName(byMobile.getNickName());
        return hospitalVisitService.update(SpringCglibBeanUtils.convert(req, HospitalVisitDto.class));
    }

    /**
     * 导出订单数据
     * @param res
     */
    public void exportOrder(HttpServletResponse res){
        HSSFWorkbook wb = exportInfo(hospitalVisitDtoList);
        try {
            String fileName = new String("计划.xls".getBytes("UTF-8"),"ISO-8859-1");
            res.setContentType("application/x-download");
            res.addHeader("Content-Disposition", "attachment;filename="+ fileName);
            OutputStream os = res.getOutputStream();
            wb.write(os);
            os.close();
        } catch (Exception e) {
        }
    }

    /**
     * 设置excel内容
     * @param hospitalDtos
     * @return
     */
    public  HSSFWorkbook exportInfo(List<HospitalVisitDto> hospitalDtos){
        try {
            HSSFWorkbook wb = new HSSFWorkbook();
            HSSFCellStyle headStyle = headStyle(wb);
            HSSFCellStyle contentStyle = contentStyle(wb);
            HSSFSheet sheet_1 =  wb.createSheet("计划");
            sheet_1.setDefaultColumnWidth(30);
            CellRangeAddress cra=new CellRangeAddress(0, 0, 0, 8);
            sheet_1.addMergedRegion(cra);

            HSSFRow headRow = sheet_1.createRow(1);
            HSSFCell head_cell_1 = headRow.createCell(0);
            head_cell_1.setCellValue("日期");
            head_cell_1.setCellStyle(headStyle);

            HSSFCell head_cell_2 = headRow.createCell(1);
            head_cell_2.setCellValue("省");
            head_cell_2.setCellStyle(headStyle);

            HSSFCell head_cell_3 = headRow.createCell(2);
            head_cell_3.setCellValue("城市");
            head_cell_3.setCellStyle(headStyle);

            HSSFCell head_cell_4 = headRow.createCell(3);
            head_cell_4.setCellValue("医院名称");
            head_cell_4.setCellStyle(headStyle);

            HSSFCell head_cell_5 = headRow.createCell(4);
            head_cell_5.setCellValue("医院类别");
            head_cell_5.setCellStyle(headStyle);

            HSSFCell head_cell_6 = headRow.createCell(5);
            head_cell_6.setCellValue("科室");
            head_cell_6.setCellStyle(headStyle);

            HSSFCell head_cell_7 = headRow.createCell(6);
            head_cell_7.setCellValue("行政主任");
            head_cell_7.setCellStyle(headStyle);

            HSSFCell head_cell_8 = headRow.createCell(7);
            head_cell_8.setCellValue("行政主任联系方式");
            head_cell_8.setCellStyle(headStyle);

            HSSFCell head_cell_9 = headRow.createCell(8);
            head_cell_9.setCellValue("经销商名称");
            head_cell_9.setCellStyle(headStyle);

            HSSFCell head_cell_10 = headRow.createCell(9);
            head_cell_10.setCellValue("经销商实际控制人");
            head_cell_10.setCellStyle(headStyle);

            HSSFCell head_cell_11 = headRow.createCell(10);
            head_cell_11.setCellValue("经销商实际控制人联系方式");
            head_cell_11.setCellStyle(headStyle);

            HSSFCell head_cell_12 = headRow.createCell(11);
            head_cell_12.setCellValue("经销商实际操作人");
            head_cell_12.setCellStyle(headStyle);

            HSSFCell head_cell_13 = headRow.createCell(12);
            head_cell_13.setCellValue("经销商实际操作人电话");
            head_cell_13.setCellStyle(headStyle);

            HSSFCell head_cell_14 = headRow.createCell(13);
            head_cell_14.setCellValue("经销商名称");
            head_cell_14.setCellStyle(headStyle);

            HSSFCell head_cell_15 = headRow.createCell(14);
            head_cell_15.setCellValue("经销商实际操作人当前在科室销售产品");
            head_cell_15.setCellStyle(headStyle);

            HSSFCell head_cell_16 = headRow.createCell(15);
            head_cell_16.setCellValue("拜访计划");
            head_cell_16.setCellStyle(headStyle);

            HSSFCell head_cell_17 = headRow.createCell(16);
            head_cell_17.setCellValue("拜访结果");
            head_cell_17.setCellStyle(headStyle);

            HSSFCell head_cell_18 = headRow.createCell(17);
            head_cell_18.setCellValue("行动计划");
            head_cell_18.setCellStyle(headStyle);
            //为内容行添加数据和样式
            SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            HSSFRow contentRow;
            HSSFCell content_cell_1;
            HSSFCell content_cell_2;
            HSSFCell content_cell_3;
            HSSFCell content_cell_4;
            HSSFCell content_cell_5;
            HSSFCell content_cell_6;
            HSSFCell content_cell_7;
            HSSFCell content_cell_8;
            HSSFCell content_cell_9;
            HSSFCell content_cell_10;
            HSSFCell content_cell_11;
            HSSFCell content_cell_12;
            HSSFCell content_cell_13;
            HSSFCell content_cell_14;
            HSSFCell content_cell_15;
            HSSFCell content_cell_16;

            for (int i = 0; i <hospitalDtos.size(); i++) {
                contentRow= sheet_1.createRow(i+2);

                content_cell_1 = contentRow.createCell(0);
                if(null!=hospitalDtos.get(i).getCreateTime()) {
                    content_cell_1.setCellValue(dateFormat.format(hospitalDtos.get(i).getCreateTime()));
                }
                content_cell_1.setCellStyle(contentStyle);

                content_cell_2 = contentRow.createCell(1);
                if(null!=hospitalDtos.get(i).getProvince()) {
                    content_cell_2.setCellValue(hospitalDtos.get(i).getProvince());
                }
                content_cell_2.setCellStyle(contentStyle);

                content_cell_3 = contentRow.createCell(2);
                if(null!=hospitalDtos.get(i).getCity()) {
                    content_cell_3.setCellValue(hospitalDtos.get(i).getCity());
                }
                content_cell_3.setCellStyle(contentStyle);

                content_cell_4 = contentRow.createCell(3);
                if(null!=hospitalDtos.get(i).getHospitalName()) {
                    content_cell_4.setCellValue(hospitalDtos.get(i).getHospitalName());
                }
                content_cell_4.setCellStyle(contentStyle);

                content_cell_5 = contentRow.createCell(4);
                if(null!=hospitalDtos.get(i).getHospitalType()) {
                    content_cell_5.setCellValue(hospitalDtos.get(i).getHospitalType());
                }
                content_cell_5.setCellStyle(contentStyle);

                content_cell_6 = contentRow.createCell(5);
                if(null!=hospitalDtos.get(i).getDepartment()) {
                    content_cell_6.setCellValue(hospitalDtos.get(i).getDepartment());
                }
                content_cell_6.setCellStyle(contentStyle);

                content_cell_7 = contentRow.createCell(6);
                if(null!=hospitalDtos.get(i).getDirector()) {
                    content_cell_7.setCellValue(hospitalDtos.get(i ).getDirector());
                }
                content_cell_7.setCellStyle(contentStyle);

                content_cell_8 = contentRow.createCell(7);
                if(null!=hospitalDtos.get(i).getDirectorPhone()) {
                    content_cell_8.setCellValue(hospitalDtos.get(i).getDirectorPhone());
                }
                content_cell_8.setCellStyle(contentStyle);

                content_cell_9 = contentRow.createCell(8);
                if(null!=hospitalDtos.get(i).getDealName()) {
                    content_cell_9.setCellValue(hospitalDtos.get(i).getDealName());
                }
                content_cell_9.setCellStyle(contentStyle);


                content_cell_10 = contentRow.createCell(9);
                if(null!=hospitalDtos.get(i).getDealerControlName()) {
                    content_cell_10.setCellValue(hospitalDtos.get(i).getDealerControlPhone());
                }
                content_cell_10.setCellStyle(contentStyle);

                content_cell_11 = contentRow.createCell(10);
                if(null!=hospitalDtos.get(i).getDealerOperatorName()) {
                    content_cell_11.setCellValue(hospitalDtos.get(i).getDealerOperatorName());
                }
                content_cell_11.setCellStyle(contentStyle);

                content_cell_12 = contentRow.createCell(11);
                if(null!=hospitalDtos.get(i).getDealerOperatorPhone()) {
                    content_cell_12.setCellValue(hospitalDtos.get(i).getDealerOperatorPhone());
                }
                content_cell_12.setCellStyle(contentStyle);

                content_cell_13 = contentRow.createCell(12);
                if(null!=hospitalDtos.get(i).getSaleProduct()) {
                    content_cell_13.setCellValue(hospitalDtos.get(i).getSaleProduct());
                }
                content_cell_13.setCellStyle(contentStyle);

                content_cell_14 = contentRow.createCell(13);
                if(null!=hospitalDtos.get(i).getVisitPlan()) {
                    content_cell_14.setCellValue(hospitalDtos.get(i).getVisitPlan());
                }
                content_cell_14.setCellStyle(contentStyle);


                content_cell_15 = contentRow.createCell(14);
                if(null!=hospitalDtos.get(i).getVisitResult()) {
                    content_cell_15.setCellValue(hospitalDtos.get(i).getVisitResult());
                }
                content_cell_15.setCellStyle(contentStyle);

                content_cell_16 = contentRow.createCell(15);
                if(null!=hospitalDtos.get(i).getActionPlan()) {
                    content_cell_16.setCellValue(hospitalDtos.get(i).getActionPlan());
                }
                content_cell_16.setCellStyle(contentStyle);
            }
            return wb;
        } catch (Exception e) {
            e.getStackTrace();
        }
        return null;
    }

    /**
     * 创建表头样式
     * @param wb
     * @return
     */

    public HSSFCellStyle headStyle(HSSFWorkbook wb){
        HSSFCellStyle headStyle = wb.createCellStyle();
        HSSFFont headFont = wb.createFont();
        headFont.setFontName("微软雅黑");
        headFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        headFont.setColor(HSSFFont.COLOR_RED);
        headStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        headStyle.setFont(headFont);
        return headStyle;
    }

    public  HSSFCellStyle topicStyle(HSSFWorkbook wb){
        HSSFCellStyle headStyle = wb.createCellStyle();
        HSSFFont headFont = wb.createFont();
        headFont.setFontName("微软雅黑");
        headFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        headFont.setColor(HSSFFont.COLOR_NORMAL);
        headFont.setFontHeightInPoints((short)22);
        headStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        headStyle.setFont(headFont);
        return headStyle;
    }

    /**
     * 创建内容行样式
     * @param wb
     * @return
     */
    public HSSFCellStyle contentStyle(HSSFWorkbook wb){
        HSSFCellStyle contentStyle = wb.createCellStyle();
        HSSFFont contentFont = wb.createFont();
        contentFont.setFontName("微软雅黑");
        contentFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
        contentFont.setColor(HSSFFont.COLOR_NORMAL);
        contentStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        contentStyle.setFont(contentFont);
        return contentStyle;
    }

}
